package com.qf.dao.Impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.domain.Goods;
import com.qf.domain.GoodsType;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public List<GoodsType> findBylevl(int i) {
        String sql = " select id,name , level, parent from tb_goods_type where level = ? ";
        try {
            List<GoodsType> query = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class), i);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询失败!!");
        }
        return null;
    }

    @Override
    public GoodsType findById(int typeid) {
        String sql = "select id,name,level,Parent from tb_goods_type where id = ?";
        try {
            GoodsType query = queryRunner.query(sql, new BeanHandler<>(GoodsType.class), typeid);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<GoodsType> findAllGoods() {
        String sql = " SELECT `name`,`level`,`Parent` FROM `tb_goods_type` ";
        try {
            List<GoodsType> query = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询失败!!");
        }
        return null;
    }

    @Override
    public void addGoodsType(String level, String name) {
        String sql = " INSERT INTO `tb_goods_type` (`name`,`level`) VALUES (?,?) ";
        try {
            queryRunner.update(sql,name,level);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败!!");
        }
    }
}
